using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Cou;
using net_work.Data.Entity.Cou;
using net_work.Data.POCO;
using net_work.Data.VO.Cou;
using net_work.IRepository.Cou;
using net_work.IRepository.Sys;
using net_work.IService.Cou;
using net_work.IService.ISystem;

namespace net_work.Service.Cou;

public class CourseService(
    ICourseRepository courseRepository,
    ICollegeRepository collegeRepository,
    IExcelExportService excelExportService)
    : ICourseService
{
    public async Task<PagedResult<CourseResult>> GetCoursesPagedAsync(PagedRequest request)
    {
        return await courseRepository.GetCoursesPagedAsync(request);
    }

    public Task<Course?> SelectByIdAsync(long courseId)
    {
        return courseRepository.GetByIdAsync(courseId);
    }

    public async Task<long> InsertCourse(CourseAddRequest courseAddRequest)
    {
        var college = await collegeRepository.GetByIdAsync(courseAddRequest.CollegeId);
        if (college == null)
        {
            return -1;
        }

        return await courseRepository.InsertAsync(courseAddRequest);
    }

    public async Task<int> UpdateCourse(CourseUpdateRequest courseUpdateRequest)
    {
        if (courseUpdateRequest.Id <= 0)
        {
            return -1;
        }

        var college = await collegeRepository.GetByIdAsync(courseUpdateRequest.CollegeId);
        if (college == null)
        {
            return -1;
        }

        return await courseRepository.UpdateAsync(courseUpdateRequest) ? 1 : -1;
    }

    public async Task<int> DeleteCourseByIds(List<long> ids)
    {
        return await courseRepository.DeleteByIdsAsync(ids) ? 1 : -1;
    }

    public async Task<byte[]> ExportCourses()
    {
        var coursesPagedAsync =
            await courseRepository.GetCoursesPagedAsync(new PagedRequest(1, PageSizeEnum.Unlimited, null));

        return excelExportService.ExportToExcel(coursesPagedAsync.Data);
    }

    public async Task<List<TreeNode>?> GetCourseTree()
    {
        // 获取学院和课程数据
        var pagedResult =
            await courseRepository.GetCoursesPagedAsync(new PagedRequest(1, PageSizeEnum.Unlimited, null));
        if (pagedResult.TotalRecords <= 0)
        {
            return [];
        }

        var courses = pagedResult.Data;

        // 构造树形结构
        var collegeDict = courses
            .GroupBy(course => course.CollegeId)
            .ToDictionary(g => g.Key, g => g.First().CollegeName);

        var tree = collegeDict
            .Select(collegeResult => CreateCollegeNode(collegeResult.Key, collegeResult.Value,
                courses.Where(course => course.CollegeId == collegeResult.Key)))
            .ToList();

        return tree;
    }

    private static TreeNode CreateCollegeNode(long collegeId, string collegeName, IEnumerable<Course> courses)
    {
        return new TreeNode
        {
            Id = collegeId,
            Name = collegeName,
            Children = courses.Select(course => new TreeNode
            {
                Id = course.Id,
                Name = course.CouName,
                Children = null // 课程没有子节点
            }).ToList()
        };
    }
}